package com.fw.ui.view.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import com.fw.ui.util.UICommonUtils;
import com.fw.ui.view.common.util.PagedTableContainer;
import com.fw.ui.view.event.AbstractPageTableButtonClickListener;
import com.fw.ui.view.event.AbstractSizeTableValueChangeListener;
import com.vaadin.data.Container;
import com.vaadin.data.validator.IntegerRangeValidator;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class PagedTable extends Table {

    /**
     * 
     */
    private static final long serialVersionUID = -4338991883431712497L;

    public interface PageChangeListener {
	public void pageChanged(PagedTableChangeEvent event);
    }

    public class PagedTableChangeEvent {

	final PagedTable table;

	public PagedTableChangeEvent(PagedTable table) {
	    this.table = table;
	}

	public PagedTable getTable() {
	    return table;
	}

	public int getCurrentPage() {
	    return table.getCurrentPage();
	}

	public int getTotalAmountOfPages() {
	    return table.getTotalAmountOfPages();
	}
    }

    private List<PageChangeListener> listeners = null;

    private PagedTableContainer container;

    private ComboBox itemsPerPageSelect = new ComboBox();
    private TextField currentPageTextField = new TextField();

    public PagedTable() {
	this(null);
    }

    public PagedTable(String caption) {
	super(caption);
	setPageLength(30);
	addStyleName("pagedtable");
    }

    public TextField getCurrentPageTextField() {
	return currentPageTextField;
    }

    public void setCurrentPageTextField(TextField currentPageTextField) {
	this.currentPageTextField = currentPageTextField;
    }

    public ComboBox getItemsPerPageSelect() {
	return itemsPerPageSelect;
    }

    public void setItemsPerPageSelect(ComboBox itemsPerPageSelect) {
	this.itemsPerPageSelect = itemsPerPageSelect;
    }

    private List<Integer> pageSizes = new ArrayList<Integer>();
    private int manualCurrentPage;
    private int manualTotalPage;
    private long manualTotalElement;
    private HorizontalLayout pageManagementPageNumber = new HorizontalLayout();
    private Map<String, Button> mapPageButton = new TreeMap<String, Button>();
    private ComboBox manualItemsPerPageSelect = new ComboBox();
    private AbstractPageTableButtonClickListener pageTableButtonClickListener;
    @SuppressWarnings("unused")
    private AbstractSizeTableValueChangeListener sizeTableValueChangeListener;

    public static final String BUTTON_FIRST = "BUTTON_FIRST";
    public static final String BUTTON_PREVIOUS = "BUTTON_PREVIOUS";
    public static final String BUTTON_NEXT = "BUTTON_NEXT";
    public static final String BUTTON_LAST = "BUTTON_LAST";

    final Button pFirst = new Button("First", new ClickListener() {
	private static final long serialVersionUID = -355520120491283992L;

	public void buttonClick(ClickEvent event) {
	    manualCurrentPage = 1;
	}
    });

    final Button pPrevious = new Button("Prev", new ClickListener() {
	private static final long serialVersionUID = -355520120491283992L;

	public void buttonClick(ClickEvent event) {
	    manualCurrentPage--;
	}
    });

    final Button pNext = new Button("Next", new ClickListener() {
	private static final long serialVersionUID = -1927138212640638452L;

	public void buttonClick(ClickEvent event) {
	    manualCurrentPage++;
	}
    });

    final Button pLast = new Button("Last", new ClickListener() {
	private static final long serialVersionUID = -355520120491283992L;

	public void buttonClick(ClickEvent event) {
	    manualCurrentPage = manualTotalPage;
	}
    });

    public static final int PAGE_ELEMENT = 7;
    public static final int PAGE_HALF_ELEMENT = 4;

    public void updatePaging() {
	this.pageManagementPageNumber.removeAllComponents();
	this.mapPageButton.clear();

	int diffCurrentTotal = this.manualTotalPage - this.manualCurrentPage;

	if (this.manualCurrentPage > PAGE_HALF_ELEMENT) {
	    Label lblSepFirst = new Label("...", ContentMode.HTML);
	    lblSepFirst.addStyleName("label-lightblue");
	    this.pageManagementPageNumber.addComponent(lblSepFirst);
	    this.pageManagementPageNumber.setComponentAlignment(lblSepFirst, Alignment.TOP_LEFT);
	}

	int size = 0;
	int index = 0;
	if (diffCurrentTotal < PAGE_HALF_ELEMENT) {
	    index = this.manualTotalPage - PAGE_ELEMENT;
	} else {
	    index = this.manualCurrentPage - PAGE_HALF_ELEMENT;
	}
	if (index < 0) {
	    index = 0;
	}

	if (this.manualTotalPage < PAGE_ELEMENT) {
	    size = this.manualTotalPage;
	} else {
	    size = PAGE_ELEMENT;
	}

	for (int loop = 0; loop < size; loop++) {
	    Button pageNo = new Button("" + (index + 1));

	    pageNo.addClickListener(new ClickListener() {

		private static final long serialVersionUID = 1L;

		@Override
		public void buttonClick(ClickEvent event) {
		    Integer pageInt = (Integer) event.getButton().getData();
		    manualCurrentPage = pageInt;
		}
	    });

	    pageNo.setData(new Integer((index + 1)));
	    if (this.pageTableButtonClickListener != null) {
		pageNo.addClickListener(this.pageTableButtonClickListener);
	    }

	    int currentPage = manualCurrentPage;
	    if (currentPage == (index + 1)) {
		pageNo.setEnabled(false);
	    } else {
		pageNo.setEnabled(true);
	    }

	    pageNo.setDisableOnClick(true);
	    pageNo.setStyleName(Reindeer.BUTTON_LINK);
	    this.mapPageButton.put("" + (index + 1), pageNo);
	    this.pageManagementPageNumber.addComponent(pageNo);
	    this.pageManagementPageNumber.setComponentAlignment(pageNo, Alignment.TOP_LEFT);

	    index++;
	}

	if (diffCurrentTotal >= PAGE_HALF_ELEMENT && this.manualTotalPage > PAGE_ELEMENT) {
	    Label lblSepLast = new Label("...", ContentMode.HTML);
	    lblSepLast.addStyleName("label-lightblue");
	    this.pageManagementPageNumber.addComponent(lblSepLast);
	    this.pageManagementPageNumber.setComponentAlignment(lblSepLast, Alignment.TOP_LEFT);
	}

	if (!mapPageButton.isEmpty()) {
	    for (Entry<String, Button> entry : mapPageButton.entrySet()) {
		if (!entry.getKey().equals("" + manualCurrentPage)) {
		    entry.getValue().setEnabled(true);
		} else {
		    entry.getValue().setEnabled(false);
		}
	    }
	}
	if (manualCurrentPage == 1) {
	    pFirst.setEnabled(false);
	    pPrevious.setEnabled(false);
	} else {
	    pFirst.setEnabled(true);
	    pPrevious.setEnabled(true);
	}

	if (manualCurrentPage == manualTotalPage) {
	    pNext.setEnabled(false);
	    pLast.setEnabled(false);
	} else {
	    pNext.setEnabled(true);
	    pLast.setEnabled(true);
	}

	if (manualTotalPage == 0 || manualTotalPage == 1) {
	    pFirst.setEnabled(false);
	    pPrevious.setEnabled(false);
	    pNext.setEnabled(false);
	    pLast.setEnabled(false);
	}
    }

    public void initPageSize(String[] pageSizes) {
	for (String size : pageSizes) {
	    manualItemsPerPageSelect.addItem("" + size);
	}
    }

    Label itemsPerPageLabel1 = new Label("Display:&nbsp;", ContentMode.HTML);
    Label itemsPerPageLabel2 = new Label("&nbsp;per page", ContentMode.HTML);

    public Label getItemsPerPageLabel1() {
	return itemsPerPageLabel1;
    }

    public void setItemsPerPageLabel1(Label itemsPerPageLabel1) {
	this.itemsPerPageLabel1 = itemsPerPageLabel1;
    }

    public Label getItemsPerPageLabel2() {
	return itemsPerPageLabel2;
    }

    public void setItemsPerPageLabel2(Label itemsPerPageLabel2) {
	this.itemsPerPageLabel2 = itemsPerPageLabel2;
    }

    public VerticalLayout createControlsPageNumber() {

	manualItemsPerPageSelect.setId("manualItemsPerPageSelect" + UICommonUtils.EXCLUDE_VALUE_COMPONENT_PREFIX);

	manualItemsPerPageSelect.setImmediate(true);
	manualItemsPerPageSelect.setNewItemsAllowed(false);
	manualItemsPerPageSelect.setTextInputAllowed(false);
	manualItemsPerPageSelect.setNullSelectionAllowed(false);
	manualItemsPerPageSelect.setWidth("70px");
	manualItemsPerPageSelect.select("30");
	manualItemsPerPageSelect.setStyleName(Reindeer.TEXTFIELD_SMALL);
	manualItemsPerPageSelect.addValueChangeListener(new ValueChangeListener() {
	    private static final long serialVersionUID = 1L;

	    public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
		int selectPagelength = Integer.parseInt((String) event.getProperty().getValue());
		setPageLength(selectPagelength);
	    }
	});

	VerticalLayout controlBar = new VerticalLayout();
	HorizontalLayout pageSize = new HorizontalLayout();
	HorizontalLayout pageManagement = new HorizontalLayout();

	pFirst.setEnabled(false);
	pPrevious.setEnabled(false);
	pNext.setEnabled(false);
	pLast.setEnabled(false);

	pFirst.setData(PagedTable.BUTTON_FIRST);
	pPrevious.setData(PagedTable.BUTTON_PREVIOUS);
	pNext.setData(PagedTable.BUTTON_NEXT);
	pLast.setData(PagedTable.BUTTON_LAST);

	pFirst.setStyleName(Reindeer.BUTTON_LINK);
	pPrevious.setStyleName(Reindeer.BUTTON_LINK);
	pNext.setStyleName(Reindeer.BUTTON_LINK);
	pLast.setStyleName(Reindeer.BUTTON_LINK);

	pFirst.addStyleName("pagedtable-first");
	pPrevious.addStyleName("pagedtable-previous");
	pNext.addStyleName("pagedtable-next");
	pLast.addStyleName("pagedtable-last");

	pFirst.addStyleName("pagedtable-button");
	pPrevious.addStyleName("pagedtable-button");
	pNext.addStyleName("pagedtable-button");
	pLast.addStyleName("pagedtable-button");

	itemsPerPageLabel1.addStyleName("pagedtable-itemsperpagecaption");
	itemsPerPageLabel2.addStyleName("pagedtable-itemsperpagecaption");
	manualItemsPerPageSelect.addStyleName("pagedtable-itemsperpagecombobox");

	itemsPerPageLabel1.addStyleName("pagedtable-label");
	itemsPerPageLabel2.addStyleName("pagedtable-label");
	manualItemsPerPageSelect.addStyleName("pagedtable-combobox");

	pageSize.addComponent(itemsPerPageLabel1);
	pageSize.addComponent(manualItemsPerPageSelect);
	pageSize.addComponent(itemsPerPageLabel2);
	pageSize.setComponentAlignment(itemsPerPageLabel1, Alignment.MIDDLE_LEFT);
	pageSize.setComponentAlignment(manualItemsPerPageSelect, Alignment.MIDDLE_LEFT);
	pageSize.setComponentAlignment(itemsPerPageLabel2, Alignment.MIDDLE_LEFT);
	pageSize.setSpacing(true);

	pageManagement.addComponent(pFirst);
	pageManagement.addComponent(pPrevious);
	pageManagement.addComponent(pageManagementPageNumber);
	pageManagement.addComponent(pNext);
	pageManagement.addComponent(pLast);

	pageManagement.setWidth(null);
	pageManagement.setSpacing(false);
	pageManagementPageNumber.setWidth(null);
	pageManagementPageNumber.setSpacing(false);

	controlBar.addComponent(pageManagement);
	controlBar.addComponent(pageSize);
	controlBar.setComponentAlignment(pageManagement, Alignment.MIDDLE_LEFT);
	controlBar.setComponentAlignment(pageSize, Alignment.MIDDLE_LEFT);
	controlBar.setWidth("99%");
	controlBar.setSpacing(true);
	controlBar.setExpandRatio(pageSize, 1);

	return controlBar;
    }

    public HorizontalLayout createControls() {
	itemsPerPageSelect.addItem("30");
	itemsPerPageSelect.addItem("50");
	itemsPerPageSelect.addItem("100");
	itemsPerPageSelect.addItem("500");
	itemsPerPageSelect.setImmediate(true);
	itemsPerPageSelect.setNewItemsAllowed(false);
	itemsPerPageSelect.setTextInputAllowed(false);
	itemsPerPageSelect.setNullSelectionAllowed(false);
	itemsPerPageSelect.setWidth("70px");
	itemsPerPageSelect.addValueChangeListener(new ValueChangeListener() {
	    private static final long serialVersionUID = -2255853716069800092L;

	    @SuppressWarnings("rawtypes")
	    public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
		int selectPagelength = Integer.parseInt((String) event.getProperty().getValue());
		Collection dataList = container.getItemIds();
		if (dataList == null) {
		    setPageLength(1);
		} else {
		    if (dataList.size() > selectPagelength) {
			setPageLength(selectPagelength);
		    } else {
			if (dataList.isEmpty() || dataList.size() == 0) {
			    setPageLength(1);
			} else {
			    setPageLength(dataList.size());
			}
		    }
		}
	    }
	});
	itemsPerPageSelect.select("30");
	itemsPerPageSelect.setStyleName(Reindeer.TEXTFIELD_SMALL);

	Label itemsPerPageLabel1 = new Label("Display&nbsp;:&nbsp;", ContentMode.HTML);
	Label itemsPerPageLabel2 = new Label("&nbsp;per page", ContentMode.HTML);

	Label pageLabel = new Label("Page:&nbsp;", ContentMode.HTML);
	// final TextField currentPageTextField = new TextField();
	currentPageTextField.setValue(String.valueOf(getCurrentPage()));
	currentPageTextField.setConverter(Integer.class);
	currentPageTextField.addValidator(new IntegerRangeValidator("Wrong page number", 1, getTotalAmountOfPages()));
	Label separatorLabel = new Label("&nbsp;/&nbsp;", ContentMode.HTML);
	final Label totalPagesLabel = new Label(String.valueOf(getTotalAmountOfPages()), ContentMode.HTML);
	currentPageTextField.setStyleName(Reindeer.TEXTFIELD_SMALL);
	currentPageTextField.setImmediate(true);
	currentPageTextField.addValueChangeListener(new ValueChangeListener() {
	    private static final long serialVersionUID = -2255853716069800092L;

	    public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
		if (currentPageTextField.isValid() && currentPageTextField.getValue() != null) {
		    int page = Integer.valueOf(String.valueOf(currentPageTextField.getValue()));
		    setCurrentPage(page);
		}
	    }
	});
	pageLabel.setWidth(null);
	currentPageTextField.setWidth("40px");
	separatorLabel.setWidth(null);
	totalPagesLabel.setWidth(null);

	HorizontalLayout controlBar = new HorizontalLayout();
	HorizontalLayout pageSize = new HorizontalLayout();
	HorizontalLayout pageManagement = new HorizontalLayout();
	final Button first = new Button("<<", new ClickListener() {
	    private static final long serialVersionUID = -355520120491283992L;

	    public void buttonClick(ClickEvent event) {
		setCurrentPage(0);
	    }
	});
	final Button previous = new Button("<", new ClickListener() {
	    private static final long serialVersionUID = -355520120491283992L;

	    public void buttonClick(ClickEvent event) {
		previousPage();
	    }
	});
	final Button next = new Button(">", new ClickListener() {
	    private static final long serialVersionUID = -1927138212640638452L;

	    public void buttonClick(ClickEvent event) {
		nextPage();
	    }
	});
	final Button last = new Button(">>", new ClickListener() {
	    private static final long serialVersionUID = -355520120491283992L;

	    public void buttonClick(ClickEvent event) {
		setCurrentPage(getTotalAmountOfPages());
	    }
	});
	first.setStyleName(Reindeer.BUTTON_LINK);
	previous.setStyleName(Reindeer.BUTTON_LINK);
	next.setStyleName(Reindeer.BUTTON_LINK);
	last.setStyleName(Reindeer.BUTTON_LINK);

	itemsPerPageLabel1.addStyleName("pagedtable-itemsperpagecaption");
	itemsPerPageLabel2.addStyleName("pagedtable-itemsperpagecaption");
	itemsPerPageSelect.addStyleName("pagedtable-itemsperpagecombobox");
	pageLabel.addStyleName("pagedtable-pagecaption");
	currentPageTextField.addStyleName("pagedtable-pagefield");
	separatorLabel.addStyleName("pagedtable-separator");
	totalPagesLabel.addStyleName("pagedtable-total");
	first.addStyleName("pagedtable-first");
	previous.addStyleName("pagedtable-previous");
	next.addStyleName("pagedtable-next");
	last.addStyleName("pagedtable-last");

	itemsPerPageLabel1.addStyleName("pagedtable-label");
	itemsPerPageLabel2.addStyleName("pagedtable-label");
	itemsPerPageSelect.addStyleName("pagedtable-combobox");
	pageLabel.addStyleName("pagedtable-label");
	currentPageTextField.addStyleName("pagedtable-label");
	separatorLabel.addStyleName("pagedtable-label");
	totalPagesLabel.addStyleName("pagedtable-label");
	first.addStyleName("pagedtable-button");
	previous.addStyleName("pagedtable-button");
	next.addStyleName("pagedtable-button");
	last.addStyleName("pagedtable-button");

	pageSize.addComponent(itemsPerPageLabel1);
	pageSize.addComponent(itemsPerPageSelect);
	pageSize.addComponent(itemsPerPageLabel2);
	pageSize.setComponentAlignment(itemsPerPageLabel1, Alignment.MIDDLE_LEFT);
	pageSize.setComponentAlignment(itemsPerPageSelect, Alignment.MIDDLE_LEFT);
	pageSize.setComponentAlignment(itemsPerPageLabel2, Alignment.MIDDLE_LEFT);
	pageSize.setSpacing(true);
	pageManagement.addComponent(first);
	pageManagement.addComponent(previous);
	pageManagement.addComponent(pageLabel);
	pageManagement.addComponent(currentPageTextField);
	pageManagement.addComponent(separatorLabel);
	pageManagement.addComponent(totalPagesLabel);
	pageManagement.addComponent(next);
	pageManagement.addComponent(last);
	pageManagement.setComponentAlignment(first, Alignment.MIDDLE_LEFT);
	pageManagement.setComponentAlignment(previous, Alignment.MIDDLE_LEFT);
	pageManagement.setComponentAlignment(pageLabel, Alignment.MIDDLE_LEFT);
	pageManagement.setComponentAlignment(currentPageTextField, Alignment.MIDDLE_LEFT);
	pageManagement.setComponentAlignment(separatorLabel, Alignment.MIDDLE_LEFT);
	pageManagement.setComponentAlignment(totalPagesLabel, Alignment.MIDDLE_LEFT);
	pageManagement.setComponentAlignment(next, Alignment.MIDDLE_LEFT);
	pageManagement.setComponentAlignment(last, Alignment.MIDDLE_LEFT);
	pageManagement.setWidth(null);
	pageManagement.setSpacing(true);
	controlBar.addComponent(pageSize);
	controlBar.addComponent(pageManagement);
	controlBar.setComponentAlignment(pageManagement, Alignment.MIDDLE_RIGHT);
	controlBar.setWidth("99%");
	controlBar.setExpandRatio(pageSize, 1);
	addListener(new PageChangeListener() {
	    public void pageChanged(PagedTableChangeEvent event) {
		first.setEnabled(container.getStartIndex() > 0);
		previous.setEnabled(container.getStartIndex() > 0);
		next.setEnabled(container.getStartIndex() < container.getRealSize() - getPageLength());
		last.setEnabled(container.getStartIndex() < container.getRealSize() - getPageLength());
		currentPageTextField.setValue(String.valueOf(getCurrentPage()));
		totalPagesLabel.setValue(String.valueOf(getTotalAmountOfPages()));
		itemsPerPageSelect.setValue(String.valueOf(getPageLength()));
	    }
	});
	return controlBar;
    }

    @Override
    public Container.Indexed getContainerDataSource() {
	return container;
    }

    @Override
    public void setContainerDataSource(Container newDataSource) {
	if (!(newDataSource instanceof Container.Indexed)) {
	    throw new IllegalArgumentException("PagedTable can only use containers that implement Container.Indexed");
	}
	PagedTableContainer pagedTableContainer = new PagedTableContainer((Container.Indexed) newDataSource);
	pagedTableContainer.setPageLength(getPageLength());
	super.setContainerDataSource(pagedTableContainer);
	this.container = pagedTableContainer;
	firePagedChangedEvent();
    }

    private void setPageFirstIndex(int firstIndex) {
	if (container != null) {
	    if (firstIndex <= 0) {
		firstIndex = 0;
	    }
	    if (firstIndex > container.getRealSize() - 1) {
		int size = container.getRealSize() - 1;
		int pages = 0;
		if (getPageLength() != 0) {
		    pages = (int) Math.floor(0.0 + size / getPageLength());
		}
		firstIndex = pages * getPageLength();
	    }
	    container.setStartIndex(firstIndex);
	    setCurrentPageFirstItemIndex(firstIndex);
	    containerItemSetChange(new Container.ItemSetChangeEvent() {
		private static final long serialVersionUID = -5083660879306951876L;

		public Container getContainer() {
		    return container;
		}
	    });
	    if (alwaysRecalculateColumnWidths) {
		for (Object columnId : container.getContainerPropertyIds()) {
		    setColumnWidth(columnId, -1);
		}
	    }
	    firePagedChangedEvent();
	}
    }

    private void firePagedChangedEvent() {
	if (listeners != null) {
	    PagedTableChangeEvent event = new PagedTableChangeEvent(this);
	    for (PageChangeListener listener : listeners) {
		listener.pageChanged(event);
	    }
	}
    }

    @Override
    public void setPageLength(int pageLength) {
	if (pageLength >= 0 && getPageLength() != pageLength) {
	    container.setPageLength(pageLength);
	    super.setPageLength(pageLength);
	    firePagedChangedEvent();
	}
    }

    public void nextPage() {
	setPageFirstIndex(container.getStartIndex() + getPageLength());
    }

    public void previousPage() {
	setPageFirstIndex(container.getStartIndex() - getPageLength());
    }

    public int getCurrentPage() {
	double pageLength = getPageLength();
	int page = (int) Math.floor((double) container.getStartIndex() / pageLength) + 1;
	if (page < 1) {
	    page = 1;
	}
	return page;
    }

    public void setCurrentPage(int page) {
	int newIndex = (page - 1) * getPageLength();
	if (newIndex < 0) {
	    newIndex = 0;
	}
	if (newIndex >= 0 && newIndex != container.getStartIndex()) {
	    setPageFirstIndex(newIndex);
	}
    }

    public int getTotalAmountOfPages() {
	int size = container.getContainer().size();
	double pageLength = getPageLength();
	int pageCount = (int) Math.ceil(size / pageLength);
	if (pageCount < 1) {
	    pageCount = 1;
	}
	return pageCount;
    }

    public void addListener(PageChangeListener listener) {
	if (listeners == null) {
	    listeners = new ArrayList<PageChangeListener>();
	}
	listeners.add(listener);
    }

    public void removeListener(PageChangeListener listener) {
	if (listeners == null) {
	    listeners = new ArrayList<PageChangeListener>();
	}
	listeners.remove(listener);
    }

    public void setAlwaysRecalculateColumnWidths(boolean alwaysRecalculateColumnWidths) {
	this.alwaysRecalculateColumnWidths = alwaysRecalculateColumnWidths;
    }

    public ComboBox getManualItemsPerPageSelect() {
	return manualItemsPerPageSelect;
    }

    public void setManualItemsPerPageSelect(ComboBox manualItemsPerPageSelect) {
	this.manualItemsPerPageSelect = manualItemsPerPageSelect;
    }

    public int getManualTotalPage() {
	return manualTotalPage;
    }

    public void setManualTotalPage(int manualTotalPage) {
	this.manualTotalPage = manualTotalPage;
    }

    public long getManualTotalElement() {
	return manualTotalElement;
    }

    public void setManualTotalElement(long manualTotalElement) {
	this.manualTotalElement = manualTotalElement;
    }

    public int getManualCurrentPage() {
	return manualCurrentPage;
    }

    public void setManualCurrentPage(int manualCurrentPage) {
	this.manualCurrentPage = manualCurrentPage;
    }

    public void addPageTableButtonClickListener(AbstractPageTableButtonClickListener pageTableButtonClickListener) {
	this.pageTableButtonClickListener = pageTableButtonClickListener;
	this.pFirst.addClickListener(this.pageTableButtonClickListener);
	this.pPrevious.addClickListener(this.pageTableButtonClickListener);
	this.pNext.addClickListener(this.pageTableButtonClickListener);
	this.pLast.addClickListener(this.pageTableButtonClickListener);
    }

    public void addSizeTableValueChangeListener(AbstractSizeTableValueChangeListener sizeTableValueChangeListener) {
	this.sizeTableValueChangeListener = sizeTableValueChangeListener;
	this.manualItemsPerPageSelect.addValueChangeListener(sizeTableValueChangeListener);
    }

    public List<Integer> getPageSizes() {
	return pageSizes;
    }

    public Button getpFirst() {
	return pFirst;
    }

    public Button getpPrevious() {
	return pPrevious;
    }

    public Button getpNext() {
	return pNext;
    }

    public Button getpLast() {
	return pLast;
    }

}